import http from "http";
import querystring from "querystring";
import TNT_TNT from "./TNT_TNT.mjs";
import SmartisanTNT from "./SmartisanTNT.mjs";
import R2Peijian from "./R2Peijian.mjs";
import R2 from "./R2.mjs";
import JXPJ from "./JXPJ.mjs";
import home_forum from "./home_forum.mjs";
import home_dynamic from "./home_dynamic.mjs";
import home_activities from "./home_activities.mjs";
import home_carousel from "./home_carousel.mjs";
import shopcar from "./shopcar.mjs";
import BHT from "./BHT.mjs"
import TNT_GO from "./TNT_GO.mjs";
import listtest from "./listtest.mjs";
import list from "./list.mjs";
import user from "./user.mjs";
import usertest from "./usertest.mjs";

export default class HttpServer {
    constructor(port = 8080, hostname = "10.9.46.151") {
        var server = http.createServer((req, res) => this.createServer(req, res));
        server.listen(port, hostname, () => console.log("开启服务"));
    }
    createServer(req, res) {
        var param;
        var type;
        var data;
        var arr = req.url?.split("?");

        if (arr) {
            type = arr[0];
            if (arr.length > 1) param = querystring.parse(arr[1]);
        }

        req.on("data", (_data) => data += _data);
        req.on("end", () => this.endHandler(res, type, data, param));

    }
    endHandler(res, type, data, param) {
        switch (type.toLowerCase()) {
            case "/changeuser":
                user.changeuser(data)
                break;
            case "/deluser":
                user.deluser(data)
                break;
            case "/adduser":
                user.adduser(data)
                break;
            case "/usertest":
                new usertest(res);
                break;
            case "/list":
                new list(res);
                break;
            case "/listtest":
                new listtest(res);
                break;
            case "/changegoods":
                shopcar.changeshopcar(data)
                break;
            case "/tnt_go":
                new TNT_GO(res);
                break;
            case "/delgoods":
                shopcar.delshopcar(data,res)
                break;
            case "/addgoods":
                shopcar.addshopcar(data,res)
                break;
            case "/user":
                new home_carousel(res);
                break;
            case "/tnt_tnt":
                new TNT_TNT(res);
                break;
            case "/smartisantnt":
                new SmartisanTNT(res);
                break;
            case "/shopcar":
                new home_carousel(res);
                break;
            case "/r2peijian":
                new R2Peijian(res);
                break;
            case "/r2":
                new R2(res);
                break;
            case "/jxpj":
                new JXPJ(res);
                break;
            case "/home_forum":
                new home_forum(res);
                break;
            case "/home_dynamic":
                new home_dynamic(res);
                break;
            case "/home_carousel":
                new home_carousel(res);
                break;
            case "/home_activities":
                new home_activities(res)
                break;
            case "/bht":
                new BHT(res)
                break;
        }
    }

}